//
//  HWPromotionAnnouncementViewController.m
//  HWPromoteProjectAPP
//
//  Created by 陈道斌 on 2020/2/25.
//  Copyright © 2020 陈道斌. All rights reserved.
//

#import "HWPromotionAnnouncementViewController.h"
#import "HWPromotionAnnouncementCell.h"
#import "HWPromotionAnnouncementModel.h"
#import "HWWebViewGameViewController.h"
#import "HWPromoteProjectAPP-Swift.h"
@interface HWPromotionAnnouncementViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(strong,nonatomic) UITableView *mainTabelView;
@property(strong,nonatomic) NSMutableArray *dataMuArr;


@property(strong,nonatomic) NSString *p;
@property(strong,nonatomic) NSString *max_p;

@end

@implementation HWPromotionAnnouncementViewController


- (UITableView *)mainTabelView {
    
    if (_mainTabelView == nil) {
        _mainTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
        _mainTabelView.delegate = self;
        _mainTabelView.dataSource = self;
        _mainTabelView.backgroundColor = beijingyanSe;
        _mainTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        

    }

    return _mainTabelView;
    
    
    
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
     
    [self setCommonProperties];
    
    // 发送网络请求
 //   [self sendNetworkRequest];
    [self addrefreshControl];
    
    
    
    
}



// 设置常用的属性
- (void)setCommonProperties {
    
    
    self.view.backgroundColor =  beijingyanSe;
    [self.view addSubview:self.mainTabelView];
    
    
    
    
    self.navigationItem.title = @"推广公告";
    NSBundle *mainBundle = [NSBundle mainBundle];
    NSLog(@"%@",mainBundle);
    
    
    
    
    

    
}




//设置UI
- (void)setupUI {
    
    
    
    
    
}



- (void)addrefreshControl {
   
    self.mainTabelView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
   [self.mainTabelView.mj_header beginRefreshing];
    
    self.mainTabelView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}




// 下拉刷新
- (void)underRefresh {
      self.p = @"1";
      NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
      NSString *uid =  [HWLocallyStoredData getLoginUserID];
      NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tStr,@"adesxloksy13s98adv"];
      NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
      NSDictionary *parametes = @{              @"ios" : @"1",
                                           @"p" :self.p,
                                           @"ct" : @"app",
                                            @"ac" : @"news",
                                            @"t" : tStr,
                                            @"uid":uid,
                                            @"sign" :signStrMd5
                                            };
            
            
                [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
                      
                       [self.mainTabelView.mj_header endRefreshing];
                       [self.mainTabelView.mj_footer endRefreshing];

                      NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
                      if ([codeStr intValue] == 1) {
                          
                          
                          self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                          self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                          self.dataMuArr = [HWPromotionAnnouncementModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
                          [self.mainTabelView reloadData];
                           NSLog(@"%@",response);
                          
                
                          
                           
                      } else {
                          
                        [MBProgressHUD showError:response[@"message"]];
                                        
                    
                          
                      }
                      
                 
                      
                  } failure:^(NSString *error) {
                     // [SVProgressHUD dismiss];
                      [self.mainTabelView.mj_header endRefreshing];
                      [self.mainTabelView.mj_footer endRefreshing];
                      
                      
//                       [MBProgressHUD showSuccess:@"登录失败"];
//                       [MBProgressHUD hideHUD];
                      
                      
                  }];
            
        
    
      
    
    


}





// 上拉加载

- (void)onRefresh {
    
    int p = [self.p intValue];
                   
    int max_p = [self.max_p intValue];
                   
     if (p > max_p) {
         
          [self.mainTabelView.mj_header endRefreshing];
          [self.mainTabelView.mj_footer endRefreshing];

        [MBProgressHUD showError:@"没有更多数据，请待会在上拉加载"];
                       
        return ;
                       
      }
    
        NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
          NSString *uid =  [HWLocallyStoredData getLoginUserID];
          NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tStr,@"adesxloksy13s98adv"];
          NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
          NSDictionary *parametes = @{              @"ios" : @"1",
                                               @"p" :self.p,
                                               @"ct" : @"app",
                                                @"ac" : @"news",
                                                @"t" : tStr,
                                                @"uid":uid,
                                                @"sign" :signStrMd5
                                                };
                
                
                    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
                          
                           [self.mainTabelView.mj_header endRefreshing];
                           [self.mainTabelView.mj_footer endRefreshing];

                          NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
                          if ([codeStr intValue] == 1) {
                              
                              
                              self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                              self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                              NSMutableArray *mutableArr = [HWPromotionAnnouncementModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
                              for (int i = 0; i < mutableArr.count; i++) {
                                    
                                  
                                  [self.dataMuArr addObject:mutableArr[i]];

                               }
                              
                              
                              [self.mainTabelView reloadData];
                         
                                NSLog(@"%@",response);
                              
                    
                              
                               
                          } else {
                              
                            [MBProgressHUD showError:response[@"message"]];
                                            
                        
                              
                          }
                          
                     
                          
                      } failure:^(NSString *error) {
                         // [SVProgressHUD dismiss];
                          [self.mainTabelView.mj_header endRefreshing];
                          [self.mainTabelView.mj_footer endRefreshing];
                          
                          
    //                       [MBProgressHUD showSuccess:@"登录失败"];
    //                       [MBProgressHUD hideHUD];
                          
                          
                      }];
                
            
    
    
    
        
                          
    
}




// 发送网络请求
- (void)sendNetworkRequest {
    
    
    
     
         NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
         NSString *uid =  [HWLocallyStoredData getLoginUserID];
         NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tStr,@"adesxloksy13s98adv"];
         NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
           
        
        
        
          NSDictionary *parametes = @{      @"ios" : @"1",
                                       @"ct" : @"app",
                                        @"ac" : @"news",
                                        @"t" : tStr,
                                        @"uid":uid,
                                        @"sign" :signStrMd5
                                        };
        
        
            [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
                  
                 

                  NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
                  if ([codeStr intValue] == 1) {
                 
                        NSLog(@"%@",response);
                      
            
                      
                       
                  } else {
                      
                    [MBProgressHUD showError:response[@"message"]];
                                    
                
                      
                  }
                  
             
                  
              } failure:^(NSString *error) {
                 // [SVProgressHUD dismiss];
                  
                   [MBProgressHUD showSuccess:@"登录失败"];
                    [MBProgressHUD hideHUD];
                  
                  
              }];
        
    
    
    
    
    
    
    
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return  self.dataMuArr.count;
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellID";
    HWPromotionAnnouncementCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    
    if (!cell) {
        cell =  [[NSBundle mainBundle]loadNibNamed:@"HWPromotionAnnouncementCell" owner:nil options:nil].firstObject;
        
    }
    
    HWPromotionAnnouncementModel  *model  =   self.dataMuArr[indexPath.row];
    cell.model = model;
    
    
    return cell;
    
    
    
    
    
    
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
     
    
    
    return  143;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HWPromotionAnnouncementModel  *model  =   self.dataMuArr[indexPath.row];
    
    
    NSString *url = [NSString stringWithFormat:@"%@?ct=app&ac=news_info&id=%@",[HWGlobalConst baseUrl],model.id];
    
//    
    
    HWWebViewGameViewController *webview1 = [[HWWebViewGameViewController alloc]init];
     webview1.urlString = url;
    webview1.gname = @"推广员系统";
  //   webview.panduanStr = @"1";
    webview1.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:webview1 animated:YES];
    

    
}




@end
